from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import Field

from app.schemas.common import APIModel, Timestamped


class DiaryEntryBase(APIModel):
  title: Optional[str] = Field(default=None, max_length=180)
  content_md: str
  mood: Optional[str] = Field(default=None, max_length=32)
  tags: list[str] = Field(default_factory=list)
  visibility: str = Field(default='shared')


class DiaryEntryCreate(DiaryEntryBase):
  ai_prompt: Optional[str] = None
  allow_ai_summary: bool = True
  notify_partner: bool = False


class DiaryEntryUpdate(APIModel):
  title: Optional[str] = Field(default=None, max_length=180)
  content_md: Optional[str] = None
  mood: Optional[str] = Field(default=None, max_length=32)
  tags: Optional[list[str]] = None
  visibility: Optional[str] = None


class DiaryEntryResponse(DiaryEntryBase, Timestamped):
  id: int
  ai_summary: Optional[str] = None
  created_at: Optional[datetime] = None
  tags: list[str] = Field(default_factory=list, alias='tags_json')
  relationship_id: Optional[int] = None


class DiaryCommentBase(APIModel):
  content: str = Field(min_length=1, max_length=1000)
  mood: Optional[str] = Field(default=None, max_length=32)


class DiaryCommentCreate(DiaryCommentBase):
  pass


class DiaryCommentResponse(DiaryCommentBase, Timestamped):
  id: int
  user_id: int
  diary_entry_id: int
  author_name: Optional[str] = None


class DiaryEntryDetailResponse(DiaryEntryResponse):
  comments: list[DiaryCommentResponse] = Field(default_factory=list)
